<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-15px"
      label-width="85px"
    >
      <el-form-item label="任务名称" prop="name">
        <ns-input
          v-model="queryParams.name"
          class="!w-240px"
          clearable
          placeholder="请输入任务名称"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable class="!w-240px">
          <el-option label="进行中" :value="1" />
          <el-option label="错误" :value="2" />
          <el-option label="已完成" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="datetimerange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" v-hasPermi="['develop:keyword:search']" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh" v-hasPermi="['develop:keyword:search']" />
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <ContentWrap>
    <div class="handle-btns">
      <el-button type="primary" @click="openForm('create')" v-hasPermi="['develop:keyword:add']"
        >新增</el-button
      >
    </div>
    <el-table v-loading="loading" :data="list" row-key="id" border>
      <el-table-column type="index" width="80" label="序号" align="center" />
      <el-table-column prop="name" label="任务名称" align="center" />
      <el-table-column prop="createTime" width="300" label="创建时间" align="center" />
      <el-table-column prop="status" width="200" label="状态" align="center">
        <template #default="scope">
          <el-tag v-if="scope.row.status" :type="statusMap.get(scope.row.status)?.type">{{
            statusMap.get(scope.row.status)?.label || ''
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column width="150" label="错误信息" align="center">
        <template #default="scope">
          <el-button
            v-if="scope.row.status == 2"
            link
            type="primary"
            @click="openError(scope.row.errors)"
          >
            查看
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300" align="center">
        <template #default="scope">
          <el-button
            :disabled="scope.row.status == 1"
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['develop:keyword:update']"
          >
            编辑切词
          </el-button>
          <el-button
            :disabled="scope.row.status == 1 || scope.row.status == 2"
            link
            type="primary"
            @click="openForm('info', scope.row.id)"
            v-hasPermi="['develop:keyword:result']"
          >
            切词结果
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['develop:keyword:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.page"
      :total="total"
      @pagination="getList"
    />
  </ContentWrap>
  <EditKeywordDialog ref="editDialogRef" @success="getList" />
  <TaskError ref="taskErrorRef" />
</template>

<script setup lang="ts">
import { Plus } from '@element-plus/icons-vue'
import { getKeywordTaskPage, deleteKeywordTask } from '@/api/develop/keywordTask'
import EditKeywordDialog from './dialog/EditKeyword.vue'
import { statusMap } from '@/views/develop/constants'
import TaskError from '../clustering/dialog/TaskError.vue'
defineOptions({ name: 'DevelopKeyword' })

const router = useRouter()
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const queryFormRef = ref() // 搜索的表单
const total = ref(10) // 列表的总页数
const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
const queryParams = reactive({
  page: 1,
  pageSize: 10,
  name: '',
  status: undefined,
  createTime: undefined
})

const editDialogRef = ref<InstanceType<typeof EditKeywordDialog>>()
const taskErrorRef = ref<InstanceType<typeof TaskError>>()

/** 添加/修改操作 */
const openForm = (type: 'create' | 'update' | 'edit' | 'info', id?: string) => {
  if (type === 'create') {
    editDialogRef.value?.open()
  }
  if (type === 'update') {
    editDialogRef.value?.open(id!)
  }

  if (type === 'info') {
    router.push({
      name: 'KeywordResult',
      params: { rid: id! }
    })
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.page = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const { count, data } = await getKeywordTaskPage(toRaw(queryParams))
    list.value = data
    total.value = count
  } finally {
    loading.value = false
  }
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await deleteKeywordTask(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

function openError(error) {
  taskErrorRef.value?.open(error)
}
onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.handle-btns {
  margin-bottom: 20px;
}
:deep(.is-disabled.is-link) {
  color: #b1b3b8 !important;
}
</style>
